package com.jxd.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zhaoyuZhang
 * @date  2020-07-12
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.jxd.repositories")
public class ElasticsearchConfig {
    /**
     * elk集群地址
     */
    @Value("${elasticsearch.ip}")
    private String hostName;

    /**
     * 端口
     */
    @Value("${elasticsearch.port}")
    private String port;

    /**
     * 集群名称
     */
    @Value("${elasticsearch.cluster.name}")
    private String clusterName;

    @Bean
    //spring <bean id="" class = "">
    public TransportClient transportClient () throws UnknownHostException {
        //配置es的集群名称
        //http://192.168.127.12:9200/
        //可登录9200端口查看
        Settings settings = Settings.builder().put("cluster.name",clusterName).build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(
                        //设置集群地址 这个地方只有一个
                        new TransportAddress(InetAddress.getByName(hostName), Integer.parseInt(port))
                );
        return  client;
    }
}
